package com.getpebble.android.main.sections.notifications.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.CheckBox;
import android.widget.FrameLayout;
import android.widget.ImageView;
import com.getpebble.android.R;
import com.getpebble.android.common.core.trace.Analytics.MobileAppBehavior;
import com.getpebble.android.common.core.trace.Trace;
import com.getpebble.android.common.model.PblAndroidAppModel;
import com.getpebble.android.common.model.PblAndroidAppModel.Record;
import com.getpebble.android.widget.PblTextView;
import java.util.HashMap;
import java.util.Map;

public class AndroidAppView extends FrameLayout implements OnClickListener {
    private static final String TAG = AndroidAppView.class.getSimpleName();
    private CheckBox mCheckView;
    private ImageView mIconView;
    private PblTextView mNameView;
    private Record mRecord;

    public AndroidAppView(Context context) {
        super(context);
        View rootView = LayoutInflater.from(context).inflate(R.layout.view_android_app, this, true);
        this.mIconView = (ImageView) rootView.findViewById(R.id.icon);
        this.mIconView.setOnClickListener(this);
        this.mNameView = (PblTextView) rootView.findViewById(R.id.name);
        this.mNameView.setOnClickListener(this);
        this.mCheckView = (CheckBox) rootView.findViewById(R.id.checkbox);
        this.mCheckView.setOnClickListener(this);
    }

    public void setModel(Record record) {
        if (record == null) {
            Trace.warning(TAG, "record is null");
            return;
        }
        this.mRecord = record;
        this.mIconView.setImageBitmap(this.mRecord.getIcon());
        this.mNameView.setText(this.mRecord.appName);
        if (this.mRecord.allowed) {
            this.mIconView.setAlpha(1.0f);
            this.mNameView.setAlpha(1.0f);
        } else {
            this.mIconView.setAlpha(0.35f);
            this.mNameView.setAlpha(0.35f);
        }
        this.mCheckView.setChecked(this.mRecord.chosen);
        this.mCheckView.setEnabled(this.mRecord.allowed);
    }

    public void onClick(View v) {
        if (this.mRecord.allowed) {
            if (v.getId() != this.mCheckView.getId()) {
                this.mCheckView.toggle();
            }
            boolean newVal = !this.mRecord.chosen;
            Trace.debug(TAG, "package: " + this.mRecord.packageName + " setting chosen to " + newVal);
            Map<String, Object> dataFields = new HashMap();
            dataFields.put("package_name", this.mRecord.packageName);
            dataFields.put("app_name", this.mRecord.appName);
            dataFields.put("app_version", this.mRecord.appVersion);
            dataFields.put("notifications_enabled", Boolean.valueOf(newVal));
            MobileAppBehavior.logToggleNotificationsEvent(dataFields);
            PblAndroidAppModel.setChosen(this.mRecord.packageName, newVal, getContext().getApplicationContext().getContentResolver());
            return;
        }
        Trace.debug(TAG, "Not enablind notification -- it's disallowed");
    }
}
